package com.blrs.controller;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.RepeatSubmit;
import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.*;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.service.dimlndustry.IDimIndustryService;
import com.blrs.service.enterprise.info.IEnterpriseInfoService;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.blrs.domain.DimIndustry;

/**
 * 企业信息Controller
 * 
 * @author yuxiang.chen
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/enterprise/info")
public class EnterpriseInfoController extends BaseController
{
    @Autowired
    private IEnterpriseInfoService enterpriseInfoService;

    @Autowired
    private IDimIndustryService dimIndustryService;
    @Autowired
    private ISysDeptService deptService;

    //入驻审核
    @PostMapping("/enterpriseVerify")
    public AjaxResult enterpriseVerify(@RequestBody SysDept dept) {
        return toAjax(enterpriseInfoService.enterpriseVerify(dept));
    }
    @Log(title = "公海信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/highSeas/{enterpriseId}")
    public AjaxResult deleteHighSeasByEnterpriseId(@PathVariable String enterpriseId)
    {
        return toAjax(enterpriseInfoService.deleteHighSeasByEnterpriseId(enterpriseId));
    }

    /**
     * 查询企业信息列表
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        return getDataTable(list);
    }
    @GetMapping("/platformList")
    public TableDataInfo platformList(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        if (StringUtils.isNotNull(enterpriseInfo.getPlatformName())){
            //根据平台名称查询组织机构id
            SysDept sysDept = new SysDept();
            sysDept.setDeptName(enterpriseInfo.getPlatformName());
            SysDept dept = deptService.selectDeptByDeptName(sysDept);
            if (Objects.nonNull(dept)){
                enterpriseInfo.setBelongTo(String.valueOf(dept.getDeptId()));
                List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
                return getDataTable(list);
            }else {
                return getDataTable(Collections.emptyList());
            }
        }else {
            return getDataTable(Collections.emptyList());
        }
    }
    @GetMapping("/listPrivate")
    public TableDataInfo listPrivate(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoListPrivate(enterpriseInfo);
        return getDataTable(list);
    }

    @GetMapping("/listToAdmin")
    public TableDataInfo listToAdmin(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoListToAdmin(enterpriseInfo);
        return getDataTable(list);
    }

    //查询最新进展记录-用于首页列表展示
    @GetMapping("/getNewFollowList")
    public TableDataInfo getNewFollowList(EnterpriseInfo enterpriseInfo)
    {
        clearPage();
        //startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.getNewFollowList(enterpriseInfo);
        return getDataTable(list);
    }
    //查询商机最新进展记录-用于首页列表展示
    @GetMapping("/getNewBusinessFollowList")
    public TableDataInfo getNewBusinessFollowList(BusinessOpportunityInformation data)
    {
        clearPage();
        //startPage();
        List<BusinessOpportunityInformation> list = enterpriseInfoService.getNewBusinessFollowList(data);
        return getDataTable(list);
    }

    /**
     * 根据名称获取企业信息
     */
    @GetMapping(value = "/listToName")
    public TableDataInfo getlistToName(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoByEnterpriseName(enterpriseInfo);
        return getDataTable(list);
    }

    /**
     * 根据企业名称判断是否存在于公海
     */
    @GetMapping(value = "/highSeasListToName")
    public TableDataInfo getHighSeaslistToName(HighSeas data)
    {
        startPage();
        List<Object> list = enterpriseInfoService.selectHighSeasByEnterpriseName(data);
        return getDataTable(list);

    }

    /**
     * 根据企业名称判断是否存在于电销
     */
    @GetMapping(value = "/saleListToName")
    public TableDataInfo getSalelistToName(ElectricSalesInfo data)
    {
        startPage();
        List<ElectricSalesInfo> list = enterpriseInfoService.selectSaleByEnterpriseName(data);
        return getDataTable(list);

    }

    /**
     * 导出企业信息列表
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseInfo enterpriseInfo)
    {
        enterpriseInfo.setPageNum(null);
        enterpriseInfo.setPageSize(null);
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        ExcelUtil<EnterpriseInfo> util = new ExcelUtil<EnterpriseInfo>(EnterpriseInfo.class);
        util.exportExcel(response, list, "企业信息数据");
    }
    /**
     * 下载管理模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EnterpriseInfo> util = new ExcelUtil<>(EnterpriseInfo.class);
        util.importTemplateExcel(response, "企业信息数据");
    }
    /**
     * 导入企业信息数据
     */
    @Log(title = "导入客户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,@RequestParam("markAs") String markAs) throws Exception
    {
        ExcelUtil<EnterpriseInfo> util = new ExcelUtil(EnterpriseInfo.class);
        List<EnterpriseInfo> list = util.importExcel(file.getInputStream());

        String message = enterpriseInfoService.importExcel(list,markAs);
        return success(message);
    }
    /**
     * 获取企业信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:query')")
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") String enterpriseId)
    {
        return success(enterpriseInfoService.selectEnterpriseInfoByEnterpriseId(enterpriseId));
    }



    /**
     * 新增企业信息
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult add(@RequestBody EnterpriseInfo enterpriseInfo)
    {
        return toAjax(enterpriseInfoService.insertEnterpriseInfo(enterpriseInfo));
    }
    @GetMapping(value = "/getMaxEnterpriseNo")
    public String getMaxEnterpriseNo(String belongTo){
        belongTo = SecurityUtils.getOrgId().toString();
        return enterpriseInfoService.getMaxEnterpriseNo(belongTo);
    }
    /**
     * 修改企业信息
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult edit(@RequestBody EnterpriseInfo enterpriseInfo)
    {
        return toAjax(enterpriseInfoService.updateEnterpriseInfo(enterpriseInfo));
    }


    /**
     * 删除企业信息
     */
    //@PreAuthorize("@ss.hasPermi('enterprise:info:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable String[] enterpriseIds)
    {
        return toAjax(enterpriseInfoService.deleteEnterpriseInfoByEnterpriseIds(enterpriseIds));
    }

    @PostMapping("/editMainClass")
    public AjaxResult editMainClass(@RequestBody BusinessMainClassVo businessMainClassVo) {
        return toAjax(enterpriseInfoService.editMainClass(businessMainClassVo));
    }

    @PostMapping("/editBusinessManager")
    public AjaxResult editBusinessManager(@RequestBody BusinessMainClassVo businessMainClassVo) {
        return toAjax(enterpriseInfoService.editBusinessManager(businessMainClassVo));
    }
    /**
     * 分页查询客户管理中的所有供应商类型数据
     */
    @GetMapping("/supplierList")
    public TableDataInfo getSupplierList(EnterpriseInfo enterpriseInfo)
    {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.getSupplierList(enterpriseInfo);
        //使用工具类进行处理结果集，直接返回
        return getDataTable(list);

    }

    /**
     * 查询当前机构所有的业务经理
     */
    @GetMapping("/getBusinessManagerList")
    public List<SysUser> getBusinessManagerList()
    {
        return enterpriseInfoService.getBusinessManagerList();
    }


    /**
     * 根据id查询客户/电销/公海的客户地址
     */
    @GetMapping("/getAddressById/{enterpriseId}")
    public AjaxResult getAddressById(@PathVariable("enterpriseId") String enterpriseId)
    {
        return success(enterpriseInfoService.getAddressById(enterpriseId));
    }


}
